require(['grid', 'topBar', 'layout','select2'], function () {
    var editDialog,funName,$top = $('#top'),$grid = $('#grid'),reloadGridData;

    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                {
                    text: '所属企业',
                    name: 'corpId',
                    option: {
                        url: '/sysCorpInfo/getCorpTree.htm',
                        name: 'corpName',
                        idKey: 'corpId',
                        pIdKey: 'parentCorpId'
                    },
                    width: 150,
                    type: 'text-tree'
                },
                {
                    text: '线路名称',
                    name: 'lineName',
                    width: 80,
                    type: 'text'
                },
                {
                    text: '站点编号/站点名称/站点别名',
                    name: 'qStnCd',
                    width: 80,
                    type: 'text'
                },
                // {
                //     text: '站点名称',
                //     name: 'qStnName',
                //     width: 80,
                //     type: 'text'
                // },
                // {
                //     text: '站点别名',
                //     name: 'qStnAlias',
                //     width: 80,
                //     type: 'text'
                // },
                {
                    text: '查询',
                    type: 'btn',
                  //authority: '4cd408ca665b4c4f9d2d1cff17110249',
                    click: function () {
                        // console.info($("[name=lineName]input").val());
                        // if($("[name=lineName]input").val().length>0){
                        //     console.info(true);
                        // }
                    	reloadGridData();
                    }
                }

            ],
            right: [
                {
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                          //authority: '4183cb0111d04503b07a59568abcdefg',
                            click: function () {
                            	exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                          //authority: '4183c11111d04503b07a59568abcdefg',
                            click: function () {
                            	exportFile('Pdf');
                            }
                        }
                    ]
                },
                {
                    text: '删除',
                    type: 'btn',
                    width: '698',
                    authority: '',
                    click: function () {
                    	deleteObj(true);
                    }
                },
                {
                    text: '+添加',
                    type: 'btn',
                  //authority: 'dcb435e3543441899b5e85e0ee111a1b',
                    click: function () {
                    	getEditDialog('add','新增').dialog('exec', 'reset');
                    }
                } ,
               {
                    text: '导入',
                    type: 'btn',
                  //authority: '4183cb0e22d04503b07a59568aacde11',
                    click: function () {
                        getEditDialog2().dialog('exec', 'importLgtLtt');
                    }
                }
            ]
        });
    }

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/stationInfo/searchStationInfoPage.htm",
            colModel: [
                {name: "stnId",label: 'stnId', key:true, hidden: true},
                {name: "stnCd",label: '站点编号', width: 100},
                {name: "stnName",label: '站点名称', width: 100},
                {name: "stnAlias",label: '站点别名', width: 100},
                {name: "direction",label: '方向', width: 80,dict: "LIT00000"},
                {name: "lgt",label: '经度', width: 100,align:'right',formatter:  function (cellvalue,options,rowObject) {
                	if(!cellvalue){
                		return '';
                	}
                	var a = cellvalue.toString().split(".");
                	var b = a[a.length-1];
                	if(b.length < 6){
                	    for(i=0;i<6-b.length;i++){
                            cellvalue = cellvalue + '0';
                        }
                	}
                	return cellvalue;
                }},
                {name: "ltt",label: '纬度', width: 100,align:'right',formatter:  function (cellvalue,options,rowObject) {
                	if(!cellvalue){
                		return '';
                	}
                	var a = cellvalue.toString().split(".");
                	var b = a[a.length-1];
                    if(b.length < 6){
                        for(i=0;i<6-b.length;i++){
                            cellvalue = cellvalue + '0';
                        }
                    }
                	return cellvalue;
                }},
                {name: "inRadius",label: '进站半径(米)', width: 100},
                {name: "outRadius",label: '出站半径(米)', width: 100},
                {name: "speedLimit",label: '限速(KM/H)', width: 100},
                {name: "road",label: '所在道路', width: 260},
                {name: "loDetailInfo",label: '位置信息', width: 260},
                {name: "remark",label: '备注', width: 100},
                {name: "operate", width: 130, label: '操作', title: false,align:'center',
                    formatter: 'operArea',
                    formatoptions: [{
                        text: '编辑',
                        css:'operate-edit',
                        click: function (data) {
                        	 getEditDialog('edit','编辑').dialog('exec', 'setVal',data.stnId);
                        }
                    }, {
                        text: '删除',
                        css:'operate-delete',
                        click: function (data) {
                        	deleteObj(false,data.stnId);
                        }
                    }]
            
                }
            ],
            multiselect: true
        });
    }

    /**
     * 获取表单对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    function getEditDialog(flag,option) {
    	funName =flag;
        option = option || {};
        if (editDialog) {
            editDialog.dialog('open');
            if (option.title) {
                editDialog.dialog('setOption', 'title', option.title);
            }
        } else {
            editDialog = $('<div>').dialog({
                title: option || '站点信息维护',
                url: './master/setting/sysSiteInfoSetEdit.htm',
                height: 550,
                width: 720,
                autoOpen: true,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                        	var _t = this;
                        	//funName :  add or edit
                        	$(_t).dialog('exec', funName,function(){
                		    	reloadGridData();
                		    	editDialog.dialog('close');
                        	});
                        }
                    },
                    {
                        text: '重置',
                        css: 'btn-cancel',
                        click: function () {
                        	$(this).dialog('exec', 'reset');
                        }
                    }
                ]
            });
        }
        return editDialog;
    }

    function getEditDialog2() {
        editDialog2 = $('<div>').dialog({
            title: '导入站点经纬度',
            url: './master/setting/sysSiteImportLgtLtt.htm',
            height: 400,
            width: 600,
            autoOpen: true,
            buttons: [
                {
                    text: '关闭',
                    css: 'btn-cancel',
                    click: function () {
                        $(this).dialog('close');
                    }
                }
            ]
        });
        return editDialog2;
    }
    
    //删除
    function deleteObj(isBatch,objId){
    	var objIds = objId;
        var stnIds = [];
    	if(isBatch){
    		objIds =  $grid.grid('getSelections');
                // $grid.grid('getGridParam', 'selarrrow').toString();
            for(var i = 0;i<objIds.length;i++){
                stnIds.push(objIds[i].stnId);
            }
            // console.info(stnIds);
    		// console.info($grid.grid('getSelections'));
	    	if(objIds == ""){
				$.messager.alert("提示", "请至少选择一条数据!");
				return false;
			}
    	}

    	$.messager.confirm('删除', '确定删除数据吗？', 'delete', function (r) {
	        if (r) {
                stnIds.push(objId);
				$.ajaxExt({
					async:true,
		    		url : "/stationInfo/delStationInfo.htm",
					data : {'stnIds' : stnIds}
				}).done(function(){
					//$.messager.alert("提示","删除成功。","success");
					reloadGridData();
					//reload();
				});
	        }
    	});
	}

    //查询grid
	reloadGridData = function(){
		 $grid.grid('setGridParam', {
             datatype: "JSON",
             page: 1,
             postData: $top.getValues()
         }).trigger("reloadGrid");
	};
	
	//导出
	function exportFile(suffix){
		if($grid.getGridParam("reccount")>0){
			var postData=$top.parent().getValues();
    		$.exportFile("/stationInfo/exportStationInfo"+suffix+".htm",postData);
		}
		else{
			$.messager.alert("警告","请先查询数据！","info");
		}
    }
	
    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();
    
    //进页面后查询
	reloadGridData();
});